<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21/0021
 * Time: 10:58
 */

namespace app\sports_admin\service;


use app\exception\RegionException;
use think\Request;
use app\sports_admin\model\Region as RegionModel;
class RegionService
{
    /*
     * 开通省
     */
    public static function openingProvince()
    {
        $params = self::getParams();
        //查询城市是否被开通 $params['city']城市id
        self::isOpening($params['city']);
        $bool = (new RegionModel())->save(['is_pass'=>1],['id'=>$params['city']]);
        if(!$bool){
            throw new RegionException([
                'msg'=>'开通失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'开通成功','code'=>200]);
    }
    /*
     * 开通市
     */
    public static function openingCity()
    {
        $params = self::getParams();
        //开通市   $params['project_id']项目id   $params['city']城市id
        $bool = (new RegionModel())->save(['is_pass'=>1,'project'=>implode(',',$params['project_id'])],['id'=>$params['city']]);
        if(!$bool){
            throw new RegionException([
                'msg'=>'开通失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'开通成功','code'=>200]);
    }
    /*
     * 开通区/县
     */
    public static function openingArea()
    {
        $params = self::getParams();
        //查询城市是否被开通 $params['city']城市id
        self::isOpening($params['city']);
        //开通县/区   $params['city']城市id
        $bool = (new RegionModel())->save(['is_pass'=>1],['id'=>$params['city']]);
        if(!$bool){
            throw new RegionException([
                'msg'=>'开通失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'开通成功','code'=>200]);
    }
    /*
     * 修改县/区信息
     */
    public static function saveInfo()
    {
        $params = self::getParams();
        $input['grade_a'] = $params['grade_a'];
        $input['grade_b'] = $params['grade_b'];
        $input['grade_c'] = $params['grade_c'];
        $input['total'] = $params['total'];
        $input['is_pass'] = $params['is_pass'];
        $bool = (new RegionModel())->save($input,['id'=>$params['id']]);
        if(!$bool){
            throw new RegionException([
                'msg'=>'修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['msg'=>'修改成功','code'=>200]);
    }
    /*
     * 获取参数信息
     */
    private static function getParams()
    {
        return Request::instance()->param();
    }
    /*
     * 查询城市是否开通
     * @param $id 城市信息id
     */
    private static function isOpening($id)
    {
        $cityInfo = RegionModel::where('is_pass',1)->where('id',$id)->find();
        if($cityInfo){
            throw new RegionException([
                'msg'=>'已开通,不需再次开通',
                'errorCode'=>400
            ]);
        }
        return ;
    }
}